package reflect;

import java.io.File;

/**
 * 实例化与当前类Test2在同一包中的所有类
 */
public class Test2 {
    public static void main(String[] args) throws Exception {
        File dir = new File(
          Test2.class.getResource(".").toURI()
        );
        String packname = Test2.class.getPackage().getName();
        System.out.println(packname);
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub : subs) {
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            System.out.println(className);
            Class cls = Class.forName(packname+"."+className);
            Object obj = cls.newInstance();
            System.out.println(obj);
        }
    }
}
